<!DOCTYPE html>
<html >
	<head>
		<meta charset="utf-8">
		<title>用户管理</title>
		<link rel="stylesheet" href="../../component/pear/css/pear.css" />
	</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">账号</label>
							<div class="layui-input-inline">
								<input type="text" name="username" placeholder="账号" class="layui-input">
							</div>
						</div>

						<div class="layui-form-item layui-inline">
							<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
								<i class="layui-icon layui-icon-search"></i>
								查询
							</button>
							<button type="reset" class="pear-btn pear-btn-md" lay-event="refresh">
								<i class="layui-icon layui-icon-refresh"></i>
								重置
							</button>
						</div>
					</div>
				</form>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="user-table" lay-filter="user-table"></table>
			</div>
		</div>

		<script type="text/html" id="user-toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" onclick="addUser()" >
		        <i class="layui-icon layui-icon-add-1"></i>
		        添加用户
		    </button>

		    <!--<button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
		        <i class="layui-icon layui-icon-delete"></i>
		        删除
		    </button>-->
		</script>

		<script type="text/html" id="user-bar">
			<button class="pear-btn pear-btn-primary pear-btn-sm" onclick="userEdit({{d.id}})"><i class="layui-icon layui-icon-edit"></i></button> &nbsp;
<!--			<button class="pear-btn pear-btn-primary pear-btn-sm" onclick="userInfo({{d.id}})"><i class="layui-icon layui-icon-username"></i></button> &nbsp;-->
			<button  class="pear-btn pear-btn-danger pear-btn-sm"  onclick="deleteUser({{d.id}})" ><i class="layui-icon layui-icon-delete"></i></button> &nbsp;
		</script>

		<script type="text/html" id="user-enable">
			{{#if (d.status == 0) { }}
			<input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="user-enable" checked = "checked">
			{{# }else if(d.status == 1){ }}
			<input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="user-enable" >
			{{# } }}

		</script>


		<script type="text/html" id="user-status">
			{{#if (d.status == 0) { }}
		    <span>正常</span>
		    {{# }else if(d.status == 1){ }}
		    <span>冻结</span>
		    {{# } }}
		</script>

		<script type="text/html" id="user-roleId">
			{{#if (d.roleId == 1) { }}
			<span>超级管理员</span>
			{{# }else if(d.roleId == 2){ }}
			<span>普通管理员</span>
			{{# }else if(d.roleId == 3){ }}
			<span>普通用户</span>
			{{# } }}
		</script>

		<script type="text/html" id="user-createGmt">
			{{
				layui.util.toDateString(d.gmtUpdate, 'yyyy-MM-dd HH:mm:ss')
			}}
		</script>
		<script type="text/html" id="user-modGmt">
			{{layui.util.toDateString(d.modGmt, 'yyyy-MM-dd HH:mm:ss')}}
		</script>

		<script src="../../component/layui/layui.js"></script>
		<script src="../../component/pear/pear.js"></script>
		<script>
			// 编辑
			function userEdit(id){
				layer.open({
					type: 2,
					title: '修改',
					shade: 0.1,
					area: ['700px', '600px'],
					content: '/user/getOneUser/'+id
				});
			}
			// 添加用户
			function addUser(){
				layer.open({
					type: 2,
					title: '新增',
					shade: 0.1,
					area: ['500px', '400px'],
					content: '/user/registerView'
				});
			}
			// 删除用户
			function deleteUser(id) {
				layer.confirm('确定要删除该用户', {
					icon: 3,
					title: '提示'
				}, function (index) {
					layer.close(index);
					let loading = layer.load();
					layui.jquery.ajax({
						url: '/user/removeUser/' + id,
						type: 'delete',
						success: function (result) {
							console.log(result);
							if (parseInt(result.code) === 200){
								layer.msg(result.message, {icon: 1, time: 500}, function () {
									layer.close(loading);
									layui.table.reload("user-table");
								});
							}else {
								layer.close(loading);
								layer.msg(result.message, {icon: 2, time: 500}, function () {
									layui.table.reload("user-table");
								});
							}
						},
						error:function (result) {
							console.log(result);
							layer.close(index);
							layer.msg('不能进行该操作', {icon: 2, time: 500}, function () {
								layer.close(loading);
								layui.table.reload("user-table");
							});
						}
					})
				})
			}

			layui.use(['table', 'form', 'jquery','common','util'], function() {
				let table = layui.table;
				let form = layui.form;
				let util = layui.util;
				let $ = layui.jquery;
				let common = layui.common;
				let MODULE_PATH = "view/system/operate/";


				let cols = [
					[{
							type: 'checkbox'
						},
						{
							title: 'ID',
							field: 'id',
							align: 'center',
							width: 80
						},
						{
							title: '账号',
							field: 'username',
							align: 'center',
							width: 100
						},
						{
							title: '密码',
							field: 'password',
							align: 'center'
						},
						{
							title: 'salt',
							field: 'salt',
							align: 'center'
						},
						{
							title: '状态',
							field: 'status',
							align: 'center',
							templet: '#user-enable'
						},
						{
							title: '角色',
							field: 'roleId',
							align: 'center',
							sort: true,
							templet: '#user-roleId'
						},
						{
							title: '修改时间',
							field: 'gmtUpdate',
							align: 'center'
						},
						{
							title: '注册时间',
							field: 'gmtCreate',
							align: 'center'
						},
						{
							title: '操作',
							align: 'center',
							width: 200,
							templet:'#user-bar'
						}
					]
				]


				table.render({
					elem: '#user-table',
					url: '/user/list',
					page: true,
					cols: cols,
					skin: 'line',
					toolbar: '#user-toolbar',
					defaultToolbar: [{
						title: '刷新',
						layEvent: 'refresh',
						icon: 'layui-icon-refresh',
					}, 'filter', 'print', 'exports']
				});

				table.on('tool(user-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					}
				});

				table.on('toolbar(user-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					} else if (obj.event === 'batchRemove') {
						window.batchRemove(obj);
					}
				});

				form.on('submit(user-query)', function(data) {
					table.reload('user-table', {
						url: '/user/list',
						where: data.field
					})
					return false;
				});


				// 冻结用户

				form.on('switch(user-enable)', function(obj) {
					// console.log(obj.elem.checked)
					// console.log(obj.elem.checked ? '确定要恢复该用户吗?' : '确定要冻结该用户吗?');
					// console.log(obj.elem.checked ? '0' : '1');
					// return;
					let uid=this.value;
					let message = obj.elem.checked ? '确定要恢复该用户吗?' : '确定要冻结该用户吗?';
					let status = obj.elem.checked ? '0' : '1';
					layer.confirm(message, {
						icon: 7,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: "/user/updateUser",
							type: 'post',
							data:{
								id:uid,
								status:status
							},
							dataType: 'json',
							success: function(result) {
								layer.close(loading);
								layer.msg(result.message, {
									icon: 1,
									time: 1000
								});
								layui.table.reload("user-table");
							}
						})
					});
					console.log(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
					// layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
				});

				window.add = function() {
					layer.open({
						type: 2,
						title: '新增',
						shade: 0.1,
						area: [common.isModile()?'100%':'500px', common.isModile()?'100%':'400px'],
						content: MODULE_PATH + 'add.html'
					});
				}

				window.edit = function(obj) {
					console.log(obj);
					layer.open({
						type: 2,
						title: '修改',
						shade: 0.1,
						area: ['500px', '400px'],
						content: 'user-edit'
					});
				}

				window.remove = function(obj) {
					layer.confirm('确定要删除该用户', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: MODULE_PATH + "remove/" + obj.data['userId'],
							dataType: 'json',
							type: 'delete',
							success: function(result) {
								layer.close(loading);
								if (result.success) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										obj.del();
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}

				window.batchRemove = function(obj) {
					
					var checkIds = common.checkField(obj,'userId');
					
					if (checkIds === "") {
						layer.msg("未选中数据", {
							icon: 3,
							time: 1000
						});
						return false;
					}
					
					layer.confirm('确定要删除这些用户', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: MODULE_PATH + "batchRemove/" + ids,
							dataType: 'json',
							type: 'delete',
							success: function(result) {
								layer.close(loading);
								if (result.success) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										table.reload('user-table');
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}

				window.refresh = function(param) {
					table.reload('user-table');
				}
			})
		</script>
	</body>
</html>
